package com.xhwl.logistics.util;


import com.xhwl.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用于解决车行里相关的算术问题
 */
@Slf4j
public class MathUtil {

    /**
     * 将double类型的金额转换为integer类型的金额
     */
    public static Integer amountDoubletoInteger(Double amountDouble){
        if(amountDouble!=null){
            amountDouble = amountDouble * 100;
            return amountDouble.intValue();
        }else{
            return null;
        }
    }

    /**
     * 比较两个整型是否相同
     * @param val1 需要比较的interger值1
     * @param val2 需要比较的interger值1
     * @param need 这个值是否一定不能为空
     * @param message 返回错误信息
     * @return
     */
    public static boolean equalsInterger(Integer val1, Integer val2, boolean need, String message){
        if(val1!=null&&val2!=null){
            if(StringUtils.equals(val1.toString(),val2.toString())){
                return true;
            }else{
                return false;
            }
        }else if(val1==null&&val2==null){
            if(need){
                throw new BusinessException(message);
            }
            else {
                return true;
            }
        }else{
            return false;
        }
    }

    /**
     * 通过两个整型来获取到一个百分号
     * @param total 求百分号时的被除数
     * @param free 求百分号时的除数
     * @param needSign 是否需要加%，true：加上%，false：不加上%
     * @return
     */
    public static String getRateFromInterger(Integer total, Integer free, Boolean needSign){
        if(total!=null&&free!=null){
            if(total<=0){
                if(needSign){
                    return "0.00%";
                }else {
                    return "0.00";
                }

            }
            else {
                double freeRateDouble = free.doubleValue() / total.doubleValue();
                BigDecimal freeRateDecimal = new BigDecimal(freeRateDouble * 100);
                String freeRateStr = String.valueOf(freeRateDecimal.setScale(2, BigDecimal.ROUND_DOWN).doubleValue());
                int index = freeRateStr.lastIndexOf(".");
                String substring = freeRateStr.substring(index);
                if(substring.length()==2){
                    if(needSign){
                        return freeRateStr+"0%";
                    }else{
                        return freeRateStr+"0";
                    }
                }else{
                    if(needSign){
                        return freeRateStr+"%";
                    }else{
                        return freeRateStr;
                    }
                }
            }
        }else{
            throw new BusinessException("无法对两个null值进行运算得到百分值！");
        }
    }

    /**
     * 根据很多时间的long类型，来求出平均时长，格式为HH时mm分ss秒
     */
    public static String getTimeByLong(List<Long> timeList){
        long totalTime = 0;
        for (Long aLong : timeList) {
            totalTime = totalTime + aLong;
        }
        int size = timeList.size();
        if(size==0){
            return "0时0分0秒";
        }else{
            String averStr = "";
            long averTimeLong = totalTime / size;
            long seconds = averTimeLong / 1000 % 60;
            long minute = averTimeLong / 1000 / 60 % 60;
            long hour = averTimeLong / 1000 / 3600;
            return averStr+hour+"时"+minute+"分"+seconds+"秒";
//            averTimeLong = averTimeLong/1000;
//            Date averDate = new Date(averTimeLong);;
//            SimpleDateFormat averTime = new SimpleDateFormat("HH时mm分ss秒");
//            return averTime.format(averDate);
        }
    }

    /**
     * 对list类型的String进行去重
     */
    public static List<String> noRepetFromString(List<String> originalList){
        List<String> targetList = new LinkedList<>();
        for (String plateNumber : originalList) {
            String plate = targetList.stream().filter(s -> {
                if (StringUtils.equals(s, plateNumber)) {
                    return true;
                } else {
                    return false;
                }
            }).findAny().orElse(null);
            if(plate == null){
                targetList.add(plateNumber);
            }
        }
        return targetList;
    }

    /**
     * 驼峰转下划线
     * @param str   目标字符串
     * @return: java.lang.String
     */
    public static String humpToUnderline(String str) {
        String regex = "([A-Z])";
        Matcher matcher = Pattern.compile(regex).matcher(str);
        while (matcher.find()) {
            String target = matcher.group();
            str = str.replaceAll(target, "_"+target.toLowerCase());
        }
        return str;
    }

}
